package LCR;

import java.util.Arrays;

public class nextPermutation {
    public static void main(String[] args) {
        int[] nums = new int[]{2};
        nextPermutation(nums);
    }
    public static void nextPermutation(int[] nums) {

        int n = nums.length - 1;
        int cur = n;
        int dest = cur ;

        // 找到比cur小的数
        while (cur >= 0  && nums[dest] > nums[cur]  ) {
            dest--;
            if (dest < 0) {
                cur--;
                dest = cur - 1;
            }
        }

        if(dest < 0) {
            dest = 0;
        }
        // 交换cur 和 dest 的值
        int tem = nums[cur];
        nums[cur] = nums[dest];
        nums[dest] = tem;

        Arrays.sort(nums, dest, n);


    }
}
